package m202311.d08;

/**
 * @author haocongshun
 * @date 2023/11/08 19:38:10
 */

/**
 * 58. 最后一个单词的长度
 */
public class Q58 {
    public int lengthOfLastWord(String s) {
        int startIndex = -1;
        int endIndex = -1;
        for (int i = s.length() - 1; i >= 0; i--) {
            char c = s.charAt(i);
            if (c != ' ' && endIndex == -1) {
                endIndex = i;
            } else if (endIndex != -1 && c == ' ') {
                startIndex = i;
                break;
            }
        }
        return endIndex - startIndex;
    }
}
